package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public DeliveryAddress getDaByID(Integer userId) throws SQLException {
        DeliveryAddress da=null;
        String sql = "select * from deliveryaddress where userId=? order by daId";

        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,userId);
            rs = pst.executeQuery();

            while (rs.next()) {
                da = new DeliveryAddress();
                da.setDaId(rs.getInt("daId"));
                da.setContactName(rs.getString("contactName"));
                da.setContactSex(rs.getInt("contactSex"));
                da.setContactTel(rs.getString("contactTel"));
                da.setAddress(rs.getString("address"));
                da.setUserId(rs.getString("userId"));
            }
        }finally {
            DBUtil.close(rs,pst);
        }

        return da;
    }
}
